﻿using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RoomPlayersPanel : MonoBehaviour
{
    public Button m_ReadyButton;
    public Button m_CancleReadyButton;
    public Button m_StartGameButton;
    public GameObject m_List;
    public RoomPlayerItem m_PlayerItemPerfab;

    public List<RoomPlayerItem> m_PlayerItems = new List<RoomPlayerItem>();
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        var roomManager = OnlineManager.singleton as OnlineManager;
        if (!roomManager)
            return;
        for(int i=0; i< roomManager.roomSlots.Count; ++i)
        {
            bool hasPlayer = false;
            for(int j=0; j< m_PlayerItems.Count; ++j)
            {
                var palyer = roomManager.roomSlots[i] as PlayerNetwork;
                if (m_PlayerItems[j].m_Player == palyer)
                {
                    hasPlayer = true;
                }
            }
            if(hasPlayer == false)
            {
                RoomPlayerItem item = Instantiate(m_PlayerItemPerfab);
                item.m_Player = roomManager.roomSlots[i] as PlayerNetwork;
                item.transform.SetParent(m_List.transform);
                item.transform.localScale = new Vector3(1,1,1);
                item.m_Panel = this;
                m_PlayerItems.Add(item);
            }
        }
        if (roomManager.allPlayersReady != m_StartGameButton.gameObject.activeSelf)
        {
            m_StartGameButton.gameObject.SetActive(roomManager.allPlayersReady);
        }

    }

    public void OnReadyClicked()
    {
        var networkPlayer = Player.Instance.m_PlayerNetwork;
        if (networkPlayer)
            networkPlayer.CmdChangeReadyState(true);
        m_ReadyButton.gameObject.SetActive(false);
        m_CancleReadyButton.gameObject.SetActive(true);


    }
    public void OnCancleReadyClicked()
    {
        var networkPlayer = Player.Instance.m_PlayerNetwork;
        if (networkPlayer)
            networkPlayer.CmdChangeReadyState(false);
        m_CancleReadyButton.gameObject.SetActive(false);
        m_ReadyButton.gameObject.SetActive(true);
    }
    public void OnDisconnectClicked()
    {
        // stop host if host mode
        if (NetworkServer.active && NetworkClient.isConnected)
        {
            OnlineManager.singleton.StopHost();
            OnlineManager.singleton.GetComponent<RoomListFindout>().StopDiscovery();
            Debug.Log("Stop host");
        }
        // stop client if client-only
        else if (NetworkClient.isConnected)
        {
            OnlineManager.singleton.StopClient();
            Debug.Log("Stop Client");
        }
        // stop server if server-only
        else if (NetworkServer.active)
        {
            OnlineManager.singleton.StopServer();
            OnlineManager.singleton.GetComponent<RoomListFindout>().StopDiscovery();
            Debug.Log("Stop Server");
        }
    }
    public void OnStartClicked()
    {
        var roomManager = OnlineManager.singleton as OnlineManager;
        if(roomManager.allPlayersReady)
        {
            if(roomManager.roomSlots.Count < 2)
            {
                MessageShow.Instance.Messaage("你不可以自己跟自己打！");
                return;
            }
            roomManager.ChangeToGame();
        }
        else
        {
            MessageShow.Instance.Messaage("还有人没有准备！");
        }
    }
}
